#include <iostream>
#include <log4cpp/Appender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Category.hh>
using namespace std;
using namespace log4cpp;

Appender* InitConsoleAppender() {
    OstreamAppender* app = new OstreamAppender("console", &std::cout);
    PatternLayout* layout = new PatternLayout();
    layout->setConversionPattern("%d %c [%p] %m%n");
    app->setLayout(layout);
    return app;
}

Appender* InitFileAppender() {
    FileAppender* app = new FileAppender("file", "file.log");
    PatternLayout* layout = new PatternLayout();
    layout->setConversionPattern("%d %c [%p] %m%n");
    app->setLayout(layout);
    return app;
}

Appender* InitRollingFileAppender() {
    RollingFileAppender* app = new RollingFileAppender("rollingfile", "rollingfile.log", 10 * 1024, 3);
    PatternLayout* layout = new PatternLayout();
    layout->setConversionPattern("%d %c [%p] %m%n");
    app->setLayout(layout);
    return app;
}

void setupLogger(){
    Category& root = Category::getRoot();
    root.addAppender(InitConsoleAppender());
    root.addAppender(InitFileAppender());
    root.addAppender(InitRollingFileAppender());
    root.setPriority(Priority::DEBUG);
}

int main(){   
    setupLogger();
    Category::getRoot().warn("同时输出到所有目标");

    return 0;
}

